source pdb.tcl
source traj.tcl

## Search the base directory,workingDir, and search directory for a given file.
# If found, a link will be made as required/fname which point to the destiny file.
oo::define Charmm method search { fname } {
    my variable configuration
    set olddir [pwd]
    cd $configuration(dir)
    if {[string compare [file pathtype $fname] absolute] == 0} { return $fname}
    #look under working directory
    if [file exists [file join $configuration(dir) $fname] ] { 
        puts "[self] found $fname in $configuration(dir)"
        return $fname
     }
     #look under base directory of the simulation
    if [file exists $configuration(base)/$fname] { 
        puts "[self] found $fname in $configuration(base)"
        if [file exists $configuration(dir)/required] { exec rm $configuration(dir)/required}
        exec ln -s $configuration(base) $configuration(dir)/required 
        return required/$fname
    }
    #look under the search directory of the simulation
    if [info exists configuration(search)] {
        foreach dir $configuration(search) {
            if [file exists [file join $dir $fname]] { 
                if [file exists $configuration(dir)/required] { exec rm $configuration(dir)/required}
                exec ln -s $configuration(base) $configuration(dir)/required 
                return required/$fname
            }
	    }
    }
    cd $olddir
    return ""
}

## Load the given file into CHARMM.
oo::define Charmm method load { type fname} {
    my variable loaded
    my variable configuration
    set fname [my search $fname]
    if {[string length $fname] == 0 } {    error "$fname not found in current directory [pwd]\n or $configuration(dir)"}
#TEST puts "[self] $fname"
    switch $type {
        rtf {
            my sendCmd "read rtf card name $fname\n"
            set loaded(rtf) $fname
        }
        para {
            my sendCmd "read para card name $fname\n"
            set loaded(para) $fname
        }
        psf {
            if { [info exists loaded(rtf)] && [info exists loaded(para)]} {
                my sendCmd "read psf card name $fname"
            } else {
                puts "Warning: parameter&topology file haven't been loaded! "
            }
        }
        coor {
            my sendCmd "read coor card name $fname"
        }
        pdb {
            my sendCmd "read coor pdb card name $fname"
        }
        default {
            error "Unrecognized file type $type !!!"
        }
    }
}


define Charmm method save { type fname} {
    my variable loaded
    my variable configuration
    switch $type {
        rtf {
            my sendCmd "write rtf card name $fname\n"
        }
        para {
            my sendCmd "write para card name $fname\n"
        }
        psf {
            my sendCmd "write psf card name $fname"
        }
        coor {
            my sendCmd "write coor card name $fname"
        }
        pdb {
            my sendCmd "write coor pdb card name $fname"
        }
        default {
            error "Unrecognized file type $type !!!"
        }
    }
}




define Charmm method stream { fname} {
        my variable configuration
                set fname [my search $fname]
                if {[string length $fname] == 0 } {    error "$fname not found in current directory [pwd]\n or $configuration(dir)"}
        set sfile [open $fname r]
                set script [read $sfile ]
                close $sfile
                my sendCmd $script
}

